package com.pickup.pool;


import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Data
@Component
public class ThreadPoolUtil {

    // 核心线程数
    private int corePoolSize = 5;

    // 最大线程数
    private int maxPoolSize = Runtime.getRuntime().availableProcessors();

    // 存活时间
    private long keepAliveSecond = 60;

    // 时间单位
    private TimeUnit timeUnit = TimeUnit.MINUTES;

    // 队列容量
    private int queueCapacity = 30;

    // 任务队列
    //private BlockingQueue<E> deque = new LinkedBlockingDeque<>(queueCapacity);

    // 线程工厂
    private ThreadFactory threadFactory = Executors.defaultThreadFactory();

    // 拒决策略
    private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardOldestPolicy();

    // 线程池
    private ExecutorService threadPool;


    //private volatile static ThreadPoolUtil threadPoolUtil;

    public ExecutorService getExecutorPool() {
        if (threadPool == null){
            this.threadPool =  new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSecond,
                    timeUnit, new LinkedBlockingDeque<>(queueCapacity), threadFactory, rejectedExecutionHandler);
        }
        return threadPool;
    }

//    public static ThreadPoolUtil getPoolUtil() {
//        if (threadPoolUtil == null) {
//            synchronized (ThreadPoolUtil.class) {
//                if (threadPoolUtil == null) {
//                    threadPoolUtil = new ThreadPoolUtil();
//                }
//            }
//        }
//        return threadPoolUtil;
//    }


}
